import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { Alert } from "react-native";
import { z } from "zod";

const BaseSchema = z.object({
    startDate: z.string().min(1, '开始日期不能为空'),
    endDate: z.string().min(1, '结束日期不能为空'),
    type: z.union([
        z.array(z.string()).nonempty('请至少选择一个请假类型'), // 数组不能为空
        z.string().min(1, '请选择一个请假类型') // 字符串不能为空
    ]),
    remark: z.string().optional(),
    position: z.array(z.object({
        item: z.string().min(1, '请输入地址'),
    }))
});

// 从 Schema 自动推断出 TypeScript 类型
type BaseData = z.infer<typeof BaseSchema>;

export function useLeaveForm() {
    const methods = useForm<BaseData>({
        resolver: zodResolver(BaseSchema),
        defaultValues: {
            startDate: '',
            endDate: '',
            type: [],
            remark: '',
            position: [{ item: ""}]
        }
    });
    const onSubmit = (data: BaseData) => {
        // 这里的 data 已经是被 Zod 校验过并且类型安全的
        Alert.alert('创建请假单提交成功', JSON.stringify(data, null, 2));
    };
    return { methods, onSubmit };
}